package com.zzy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.product.entity.BrandEntity;
import com.zzy.product.mapper.BrandMapper;
import com.zzy.product.service.BrandService;
import com.zzy.product.service.CategoryBrandRelationService;
import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Map;


/**
 * @author KAZU
 */
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, BrandEntity> implements BrandService {

	@Autowired
	private CategoryBrandRelationService categoryBrandRelationService;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
		String key = (String) params.get("key");
		QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(key)) {
			wrapper.eq("brand_id", key).or().like("name", key);
		}
		IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }
	
	@Transactional
	@Override
	public void updateDetail(BrandEntity brand) {
		/*保证冗余字段数据的一致*/
		this.updateById(brand);
		if (!StringUtils.isEmpty(brand.getName())) {
			categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
			
			// TODO: 更新其他关联
		}
	}
	
}